package gem.model.test;

import gem.database.ReaderWriter;
import gem.model.course.Department;
import gem.model.course.ScienceDomain;

import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;

import javax.swing.JOptionPane;
import javax.swing.JTree;
import javax.swing.JScrollPane;
import javax.swing.JEditorPane;
import javax.swing.JTabbedPane;
import java.awt.GridBagLayout;
import java.awt.CardLayout;
import java.awt.GridBagConstraints;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.tree.TreePath;
import javax.swing.JComboBox;
import javax.swing.JTextField;

public class CourseEditor extends JFrame
{
	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JTree mainTree = null;
	private JScrollPane jScrollPane = null;
	private JPanel pnl_Department = null;
	private JPanel pnl_ScienceDomain = null;
	private JPanel pnl_Course = null;
	private JPanel pnl_Main = null;
	private JPanel pnl_m = null;
	private JButton btn_AddNewDptm = null;
	private JButton btn_AddNewScDom = null;
	private JLabel lbl_CurrDptm = null;
	private JButton btn_UpdateDB = null;
	private JTabbedPane tb_Main = null;
	private JComboBox cb_NewParent = null;
	private JTextField txt_CurrParent = null;
	private JButton btn_Ok = null;
	/**
	 * This method initializes mainTree	
	 * 	
	 * @return javax.swing.JTree	
	 */
	private Department getDepartment( java.util.Vector<Department> deps, String name )
	{
		for( Department d : deps )
		{
			if( d.getTitle().equals(name))
			{
				return d;
			}
		}
		return null;
	}
	private JTree getMainTree()
	{
		if (mainTree == null) {
			mainTree = new JTree( new CourseTreeModel(ReaderWriter.loadDepartmentsFully()));
			mainTree.setPreferredSize(new Dimension(400, 72));
			mainTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
				public void valueChanged(javax.swing.event.TreeSelectionEvent e)
				{
					TreePath path = e.getPath();
					System.out.println( path.getLastPathComponent().getClass() );
					if( path.getLastPathComponent() instanceof Department )
					{						
						lbl_CurrDptm.setText(path.getLastPathComponent().toString());
					}
					else if( path.getLastPathComponent() instanceof ScienceDomain )
					{
						getTxt_CurrParent().setText( path.getLastPathComponent().toString() );
					}
				}
			});
		}
		return mainTree;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane()
	{
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
			jScrollPane.setViewportView(getMainTree());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes pnl_Department	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnl_Department() {
		if (pnl_Department == null) {
			lbl_CurrDptm = new JLabel();
			lbl_CurrDptm.setText("JLabel");
			pnl_Department = new JPanel();
			pnl_Department.setLayout(new FlowLayout());
			pnl_Department.add(lbl_CurrDptm, null);
			pnl_Department.add(getBtn_AddNewScDom(), null);
		}
		return pnl_Department;
	}

	/**
	 * This method initializes pnl_ScienceDomain	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	public JPanel getPnl_ScienceDomain() {
		if (pnl_ScienceDomain == null) {
			pnl_ScienceDomain = new JPanel();
			pnl_ScienceDomain.setLayout(new FlowLayout());
			pnl_ScienceDomain.add(getTxt_CurrParent(), null);
			pnl_ScienceDomain.add(getCb_NewParent(), null);
			pnl_ScienceDomain.add(getBtn_Ok(), null);
		}
		return pnl_ScienceDomain;
	}

	/**
	 * This method initializes pnl_Course	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnl_Course() {
		if (pnl_Course == null) {
			pnl_Course = new JPanel();
			pnl_Course.setLayout(new GridBagLayout());
		}
		return pnl_Course;
	}

	/**
	 * This method initializes pnl_Main	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnl_Main() {
		if (pnl_Main == null) {
			pnl_Main = new JPanel();
			pnl_Main.setLayout(new BorderLayout());
			pnl_Main.add(getPnl_Department(), BorderLayout.NORTH);
			pnl_Main.add(getPnl_m(), BorderLayout.CENTER);
		}
		return pnl_Main;
	}

	/**
	 * This method initializes pnl_m	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnl_m() {
		if (pnl_m == null) {
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.BOTH;
			gridBagConstraints1.gridy = 1;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.weighty = 1.0;
			gridBagConstraints1.gridx = 0;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 1;
			gridBagConstraints.gridy = 0;
			pnl_m = new JPanel();
			pnl_m.setLayout(new GridBagLayout());
			pnl_m.add(getBtn_AddNewDptm(), new GridBagConstraints());
			pnl_m.add(getBtn_UpdateDB(), gridBagConstraints);
			pnl_m.add(getTb_Main(), gridBagConstraints1);
		}
		return pnl_m;
	}

	/**
	 * This method initializes btn_AddNewDptm	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtn_AddNewDptm() {
		if (btn_AddNewDptm == null) {
			btn_AddNewDptm = new JButton();
			btn_AddNewDptm.setText("Dodaj Katedru");
			btn_AddNewDptm.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					CourseTreeModel model = (CourseTreeModel)mainTree.getModel();
					String name = JOptionPane.showInputDialog("Molimo unesite ime katedre");
					if( name == null || name.equals(""))
					{
						return;
					}
					model.deps.add( new Department(name));
					// TODO ovde obavestiti preko listenera, naravno!
					mainTree.setModel(null);
					mainTree.setModel(model);
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
				}
			});
		}
		return btn_AddNewDptm;
	}

	/**
	 * This method initializes btn_AddNewScDom	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtn_AddNewScDom()
	{
		if (btn_AddNewScDom == null)
		{
			btn_AddNewScDom = new JButton();
			btn_AddNewScDom.setText("Dodaj Naucnu oblast");
			btn_AddNewScDom.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					String name = JOptionPane.showInputDialog("Unesite naucnu oblast");
					if( name == null || name.equals(""))
					{
						return;
					}
					CourseTreeModel model = (CourseTreeModel)mainTree.getModel();
					Department d = getDepartment(model.deps, lbl_CurrDptm.getText());
					if( d == null )
					{
						return;
					}
					d.addScienceDomain( new ScienceDomain(name, d ) );
					mainTree.setModel(null);
					mainTree.setModel(model);
				}
			});
		}
		return btn_AddNewScDom;
	}

	/**
	 * This method initializes btn_UpdateDB	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtn_UpdateDB() {
		if (btn_UpdateDB == null) {
			btn_UpdateDB = new JButton();
			btn_UpdateDB.setText("Azuriraj");
		}
		return btn_UpdateDB;
	}
	/**
	 * This method initializes tb_Main	
	 * 	
	 * @return javax.swing.JTabbedPane	
	 */
	public JTabbedPane getTb_Main() {
		if (tb_Main == null) {
			tb_Main = new JTabbedPane();
			tb_Main.addTab(null, null, getPnl_ScienceDomain(), null);
			tb_Main.addTab(null, null, getPnl_Course(), null);
		}
		return tb_Main;
	}
	/**
	 * This method initializes cb_NewParent	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getCb_NewParent() {
		if (cb_NewParent == null) {
			cb_NewParent = new JComboBox();
			cb_NewParent.setPreferredSize(new Dimension(300, 25));
			CourseTreeModel model = (CourseTreeModel)mainTree.getModel();
			for( Department d : model.deps )
			{
				cb_NewParent.addItem(d);
			}
		}
		return cb_NewParent;
	}
	/**
	 * This method initializes txt_CurrParent	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	public JTextField getTxt_CurrParent() {
		if (txt_CurrParent == null) {
			txt_CurrParent = new JTextField();
			txt_CurrParent.setPreferredSize(new Dimension(300, 20));
		}
		return txt_CurrParent;
	}
	/**
	 * This method initializes btn_Ok	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtn_Ok() {
		if (btn_Ok == null) {
			btn_Ok = new JButton();
			btn_Ok.setText("OK");
			btn_Ok.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					if( mainTree.getSelectionPath().getLastPathComponent() instanceof ScienceDomain )
					{
						ScienceDomain current = (ScienceDomain)mainTree.getSelectionPath().getLastPathComponent();
						//current.changeDepartment((Department)cb_NewParent.getSelectedItem());
						CourseTreeModel model = (CourseTreeModel)mainTree.getModel();
						mainTree.setModel(null);
						mainTree.setModel(model);
						System.out.println("OK");
						
					}
					else
					{
						System.out.println("Moramo izabrati Science domain");
					}
				}
			});
		}
		return btn_Ok;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				CourseEditor thisClass = new CourseEditor();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	/**
	 * This is the default constructor
	 */
	public CourseEditor()
	{
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize()
	{
		this.setSize(935, 869);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane()
	{
		if (jContentPane == null)
		{
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJScrollPane(), BorderLayout.WEST);
			jContentPane.add(getPnl_Main(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
